package com.demo.lifeinfo.common.sign;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.RegexUtils;
import com.demo.lifeinfo.R;
import com.demo.lifeinfo.app.XBaseActivity;
import com.demo.lifeinfo.entity.User;
import com.demo.lifeinfo.model.GlideApp;
import com.demo.lifeinfo.widget.TimeCount;
import com.flyco.roundview.RoundTextView;
import com.gyf.immersionbar.ImmersionBar;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.QueryListener;

/**
 * created by Raymond
 * on 2019-04-23
 */
public class LoginActivity extends XBaseActivity {
    @BindView(R.id.login_bg)
    ImageView loginBg;
    @BindView(R.id.useraccount_edit)
    EditText useraccountEdit;
    @BindView(R.id.userpassword_edit)
    EditText userpasswordEdit;
    @BindView(R.id.btn_code)
    RoundTextView btn_code;
    TimeCount timeCount;
    @BindView(R.id.tv_time)
    TextView tvTime;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this).transparentBar().init();
        //获取系统时间
        Calendar c = Calendar.getInstance();
//提取他的时钟值，int型
        int s = c.get(Calendar.HOUR_OF_DAY);
        if (s == 6 || s == 7 || s == 8 || s == 9 || s == 12 || s == 13 || s == 14 || s == 15 || s == 16 || s == 17 || s == 10 || s == 11) {
            GlideApp.with(this).load(R.mipmap.good_morning_img).centerCrop().into(loginBg);
            tvTime.setText("Morning");
        } else {
            GlideApp.with(this).load(R.mipmap.good_night_img).centerCrop().into(loginBg);
            tvTime.setText("Night");
        }


    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.btn_code)
    public void sendCode() {
        if (TextUtils.isEmpty(useraccountEdit.getText().toString())) {
            showError(getString(R.string.sign_error_null2));
            return;
        }
        if (!RegexUtils.isMobileExact(useraccountEdit.getText().toString())) {
            showError(getString(R.string.sign_error_phone));
            return;
        }
        showProgressDialog();
        BmobSMS.requestSMSCode(useraccountEdit.getText().toString(), "", new QueryListener<Integer>() {
            @Override
            public void done(Integer smsId, BmobException e) {
                dimssProgressDialog();
                if (e == null) {
                    if (timeCount == null) {
                        timeCount = new TimeCount(60000, 1000, btn_code, userpasswordEdit);
                    }
                    timeCount.start();
                } else {
                    showError(e.getMessage());
                }
            }
        });

    }

    @OnClick(R.id.login_btn)
    public void onViewClicked() {
        if (TextUtils.isEmpty(useraccountEdit.getText().toString()) || TextUtils.isEmpty(userpasswordEdit.getText().toString())) {
            showError(getString(R.string.sign_error_null));
            return;
        }
        if (!RegexUtils.isMobileExact(useraccountEdit.getText().toString())) {
            showError(getString(R.string.sign_error_phone));
            return;
        }
        showProgressDialog();
        BmobUser.signOrLoginByMobilePhone(useraccountEdit.getText().toString(), userpasswordEdit.getText().toString(), new LogInListener<User>() {

            @Override
            public void done(User user, BmobException e) {
                dimssProgressDialog();
                if (e == null) {
                    showError("登录成功");
                    finish();
                } else {
                    Log.e("xiaoqiang", e.getMessage());
                    showError(e.getMessage());
                }
            }
        });


    }

}
